<template>
  <a-modal
    title="新增任务"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="任务编码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入任务编码" v-decorator="['code', {rules: [{required: true, message: '请输入任务编码！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="工单编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择工单编号" v-decorator="['workOrderId', {rules: [{ required: true, message: '请选择工单编号！' }]}]">
                <a-select-option v-for="(item,index) in list.workOrderList" :key="index" :value="item.id">{{ item.workOrderNo }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="工序名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择工序名称" v-decorator="['workStepId', {rules: [{ required: true, message: '请选择工序名称！' }]}]">
                <a-select-option v-for="(item,index) in list.workStepList" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="产品名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择产品名称" v-decorator="['proId', {rules: [{ required: true, message: '请选择产品名称！' }]}]">
                <a-select-option v-for="(item,index) in list.proList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24">
            <a-form-item
              label="良品数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入良品数" v-decorator="['goodNum', {rules: [{required: true, message: '请输入良品数！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="不良品数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入不良品数" v-decorator="['badNum', {rules: [{required: true, message: '请输入不良品数！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="计划时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-range-picker

                :show-time="{ format: 'HH:mm' }"
                v-decorator="['startEndTime', {rules: [{required: true, message: '请选择计划时间！'}]},{initialValue:[moment('08:00','HH:mm'),moment()]}]"
                format="YYYY-MM-DD HH:mm"
                :placeholder="['开始时间', '结束时间']"
                style="width: 100%"
                @change="startEndTimeOnChange"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="实际时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-range-picker
                :show-time="{ format: 'HH:mm' }"
                v-decorator="['factTime', {rules: [{required: true, message: '请选择实际时间！'}]},{initialValue:[moment('08:00','HH:mm'),moment()]}]"
                format="YYYY-MM-DD HH:mm"
                :placeholder="['开始时间', '结束时间']"
                style="width: 100%"
                @change="factTimeOnChange"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="计划数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input
                placeholder="请输入计划数"
                v-decorator="['plaNum', {rules: [{required: true, message: '请输入计划数！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择状态" v-decorator="['status', {rules: [{ required: true, message: '请选择状态！' }]}]">
                <a-select-option v-for="(item,index) in list.statusList" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { taskAdd } from '@/api/modular/main/task/taskManage'
  import { workOrderList } from '@/api/modular/main/workorder/workOrderManage'
  import { workStepList } from '@/api/modular/main/workstep/workStepManage'
  import { proList } from '@/api/modular/main/pro/proManage'
  import moment from 'moment'
  export default {
    data () {
      return {
        taskList: [],
        list: {
          statusList: [{ 'name': '未开始', code: -1 },
            { 'name': '执行中', code: 0 },
            { 'name': '已结束', code: 1 }],
          workOrderList: [],
          workStepList: [],
          MoIdList: [],
          proList: []
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        startEndTimeDateString: '',
        factTimeDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      },
      // 初始化方法
      add (record) {
        this.visible = true
        workOrderList().then(res => {
          this.list.workOrderList = res.data
        })
        workStepList().then(res => {
          this.list.workStepList = res.data
        })
        proList().then(res => {
          this.list.proList = res.data
        })
      },

      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.startEndTime = this.startEndTimeDateString.join(';')
            values.factTime = this.factTimeDateString.join(';')
            taskAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      factTimeOnChange(date, dateString) {
        this.factTimeDateString = dateString
      },
      startEndTimeOnChange(date, dateString) {
        this.startEndTimeDateString = dateString
      },
      plaTimeOnChange(date, dateStrArr) {
        this.plaTimeString = dateStrArr.join(';')
        this.taskList.forEach(item => {
          item.plaStartTime = dateStrArr[0]
          item.plaStartTime = dateStrArr[1]
        })
      },
      handleCancel () {
        this.startEndTimeDateString = ''
        this.form.getFieldDecorator('startEndTime', { initialValue: null })
        this.factTimeDateString = ''
        this.form.getFieldDecorator('factTime', { initialValue: null })
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
